package com.lingkun.demo.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingkun.demo.sys.entity.RoleMenu;
import com.lingkun.demo.sys.entity.SysMenu;
import com.lingkun.demo.sys.entity.SysRole;
import com.lingkun.demo.sys.entity.UserRole;
import com.lingkun.demo.sys.entity.sysVo.RoleVo;
import com.lingkun.demo.sys.entity.sysVo.menuOne;
import com.lingkun.demo.sys.mapper.SysRoleMapper;
import com.lingkun.demo.sys.service.RoleMenuService;
import com.lingkun.demo.sys.service.SysMenuService;
import com.lingkun.demo.sys.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小龙
 * @since 2021-04-24
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private RoleMenuService  roleMenuService;

    @Override     //根据用户id查询角色信息
    public List<String> getRole(String id) {
        List<String> roles = baseMapper.getRole(id);
        return  roles;
    }

    @Override  //根据权限id查询角色信息
    public List<String> getPermRole(String id) {
        List<String> roles = baseMapper.getPermRole(id);
            return  roles;
    }

    @Override  //获取所有角色的集合
    public List<RoleVo> search( ) {
        List<RoleMenu> one = roleMenuService.list(null);
        Map<String,List<RoleMenu>> map  =  new HashMap<>();
        for (int i=0;i<one.size();i++){
          List<RoleMenu>  list   =  new ArrayList<>();
            if(map.containsKey(one.get(i).getRoleId())){
                list.addAll(map.get(one.get(i).getRoleId()));
            }
               list.add(one.get(i));
             map.put(one.get(i).getRoleId(),list);
         }
        List<RoleVo> roleVos = new ArrayList<>();
        List<SysRole>  roleList =  baseMapper.selectList(null);
          for (int j=0;j<roleList.size();j++){
              RoleVo  roleVo =  new RoleVo();
                roleVo.setRoleId(roleList.get(j).getRoleId());
                roleVo.setRoleName(roleList.get(j).getRoleName());
                roleVo.setRoleDesc(roleList.get(j).getRoleDesc());
                roleVo.setRoutes(map.get(roleVo.getRoleId()));
              roleVos.add(roleVo);
          }
        return roleVos;
    }



}
